package com.team9.fitness.controller;

import com.team9.fitness.service.ZRDDSSendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * 评论点赞控制器
 * 
 * 提供评论点赞相关的REST API：
 * - 点赞评论
 * - 取消点赞评论
 * - 切换点赞状态
 * - 查询点赞状态和数量
 */
@Slf4j
@RestController
@RequestMapping("/api/comment-like")
@CrossOrigin(origins = { "*" }, allowCredentials = "false")
public class CommentLikeController {

    @Autowired
    private ZRDDSSendService zrddsSendService;

    /**
     * 切换评论点赞状态
     */
    @PostMapping("/toggle")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> toggleCommentLike(@RequestBody Map<String, Object> requestBody) {

        Long userId = Long.valueOf(requestBody.get("userId").toString());
        Long commentId = Long.valueOf(requestBody.get("commentId").toString());
        
        log.info("切换评论点赞状态请求: userId={}, commentId={}", userId, commentId);

        Map<String, Object> zrddsRequestBody = new HashMap<>();
        zrddsRequestBody.put("userId", userId);
        zrddsRequestBody.put("commentId", commentId);

        return zrddsSendService.sendBusinessRequest("COMMENT_LIKE_TOGGLE", "POST", "/api/comment-like/toggle",
                userId.toString(), zrddsRequestBody)
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("message", "评论点赞状态切换成功");
                        result.put("data", response.getData());
                        log.info("评论点赞状态切换成功: userId={}, commentId={}", userId, commentId);
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "评论点赞状态切换失败");
                        log.warn("评论点赞状态切换失败: userId={}, commentId={}, error={}", userId, commentId,
                                response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("评论点赞状态切换异常: userId={}, commentId={}", userId, commentId, throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "评论点赞状态切换服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }

    /**
     * 获取用户点赞状态
     */
    @GetMapping("/status")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> getLikeStatus(@RequestParam Long userId,
            @RequestParam Long commentId) {

        log.info("获取用户评论点赞状态请求: userId={}, commentId={}", userId, commentId);

        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("userId", userId);
        requestBody.put("commentId", commentId);

        return zrddsSendService.sendBusinessRequest("COMMENT_LIKE_GET_STATUS", "GET", "/api/comment-like/status",
                userId.toString(), requestBody)
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("message", "获取评论点赞状态成功");
                        result.put("data", response.getData());
                        log.info("获取评论点赞状态成功: userId={}, commentId={}", userId, commentId);
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "获取评论点赞状态失败");
                        log.warn("获取评论点赞状态失败: userId={}, commentId={}, error={}", userId, commentId,
                                response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("获取评论点赞状态异常: userId={}, commentId={}", userId, commentId, throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "获取评论点赞状态服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }

    /**
     * 获取评论点赞数量
     */
    @GetMapping("/count/{commentId}")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> getLikeCount(@PathVariable Long commentId) {

        log.info("获取评论点赞数量请求: commentId={}", commentId);

        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("commentId", commentId);

        return zrddsSendService
                .sendBusinessRequest("COMMENT_LIKE_GET_COUNT", "GET", "/api/comment-like/count/" + commentId,
                        null, requestBody)
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("message", "获取评论点赞数量成功");
                        result.put("data", response.getData());
                        log.info("获取评论点赞数量成功: commentId={}", commentId);
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "获取评论点赞数量失败");
                        log.warn("获取评论点赞数量失败: commentId={}, error={}", commentId, response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("获取评论点赞数量异常: commentId={}", commentId, throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "获取评论点赞数量服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }
}
